package Test;

import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;


public class User extends JFrame{
    private JTextField textName,textBirth,textDeparment,textMajor,textNumber;
    private JRadioButton btnMan,btnFemale;
    private JButton btnAdd,btnDelete,btnDeleteChoose;
    private JTextArea text;
    private JCheckBox checkMember;
    private JComboBox comboxCity,comboxProvice,comboxFind,comboxSort;
    private Object provinces[] = {"江苏省","浙江省","湖南省"};
    private Object cities[][] = { {"南京市","苏州市","无锡市"},
            {"杭州市","宁波市","温州市","衢州市"},
            {"益阳市","长沙市","岳阳市","湘潭市"}
    };
    private Person persons[]=new Person[0];
    public User(){
        setTitle("Person对象信息管理");
        setBounds(200, 200, 700, 300);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.getContentPane().setLayout(null);
        JPanel left = new JPanel(new GridLayout(10,1));
        left.add(new JLabel("Student"));
        textName=new JTextField("姓名:");
        left.add(textName);
        textBirth=new JTextField("1990年1月1日");
        left.add(textBirth);
        JPanel pSex=new JPanel();
        btnMan=new JRadioButton("男");
        btnMan.setSelected(true);
        btnFemale=new JRadioButton("女");
        pSex.add(btnMan);
        pSex.add(btnFemale);
        ButtonGroup group=new ButtonGroup();
        group.add(btnMan);
        group.add(btnFemale);
        left.add(pSex);


        comboxProvice=new JComboBox(provinces);
        left.add(comboxProvice);
        comboxCity=new JComboBox(cities[0]);
        left.add(comboxCity);


        textDeparment=new JTextField("计算机系");
        left.add(textDeparment);
        textMajor=new JTextField("计算机科学与技术");
        left.add(textMajor);
        textNumber=new JTextField("031");
        left.add(textNumber);
        checkMember=new JCheckBox("团员");
        left.add(checkMember);
        left.setBounds(10, 10, 90, 250);
        this.getContentPane().add(left);


        JSplitPane split=new JSplitPane();
        split.setBounds(110, 0, 8, 300);
        this.getContentPane().add(split);


        text=new JTextArea();
        JScrollPane jp=new JScrollPane(text);
        jp.setBounds(120, 10, 560, 200);
        this.getContentPane().add(jp);


        JPanel pAdd=new JPanel();
        pAdd.setBounds(120, 220, 560, 300);
        pAdd.setLayout(new FlowLayout(FlowLayout.LEFT));
        btnAdd=new JButton("添加");
        pAdd.add(btnAdd);
        btnDelete=new JButton("删除");
        pAdd.add(btnDelete);
        btnDeleteChoose=new JButton("删除选中项");
        pAdd.add(btnDeleteChoose);
        pAdd.add(new JLabel("查找关键字"));
        String str[]={"学号","姓名","省份","院系"};
        comboxFind=new JComboBox(str);
        pAdd.add(comboxFind);
        pAdd.add(new JLabel("排序关键字"));
        comboxSort=new JComboBox(str);
        pAdd.add(comboxSort);
        this.getContentPane().add(pAdd);
//监听
        Listener listener=new Listener();
        comboxProvice.addActionListener(listener);
        btnAdd.addActionListener(listener);
        btnDelete.addActionListener(listener);
        btnDeleteChoose.addActionListener(listener);
        comboxSort.addActionListener(listener);
        comboxFind.addActionListener(listener);
        setVisible(true);


    }
    class Listener implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent e) {
            if(e.getSource()==comboxProvice){
                int i=comboxProvice.getSelectedIndex();
                comboxCity.removeAllItems();
                for(int j=0;j<cities[i].length;j++){
                    comboxCity.addItem(cities[i][j]);
                }
            }else if(e.getSource()==btnAdd){
                boolean isAdd=true;
                for(int i=0;i<persons.length;i++){
                    if(persons[i].number.equals(textNumber.getText())){
                        isAdd=false;
                    }
                }
                if(isAdd){
                    Person[] temp=new Person[persons.length+1];
                    for(int i=0;i<persons.length;i++){
                        temp[i]=persons[i];
                    }
                    temp[temp.length-1]=new Person(textName.getText(),textBirth.getText(),(String)(btnMan.isSelected()?"男":"女"),
                            comboxProvice.getSelectedItem(),comboxCity.getSelectedItem(),textDeparment.getText(),
                            textMajor.getText(),textNumber.getText(),(String)(checkMember.isSelected()?"团员":""));
                    persons=temp;
                    temp=null;
                }
                refresh(persons);
            }else if(e.getSource()==btnDelete){
                persons=new Person[0];
                refresh(persons);
            }else if(e.getSource()==btnDeleteChoose){
                String str = text.getSelectedText();
                int j=-1;
                for(int i=0;i<persons.length;i++){
                    if(persons[i].toString().equalsIgnoreCase(str)){
                        j=i;
                        break;
                    }
                }
                for (; j < persons.length-1; j++) {
                    persons[j]=persons[j+1];
                }
                Person[] temp=new Person[persons.length-1];
                for(int i=0;i<temp.length;i++){
                    temp[i]=persons[i];
                }
                persons=temp;
                temp=null;
                refresh(persons);
            }else if(e.getSource()==comboxSort){
                String str=(String) comboxSort.getSelectedItem();
                if(str.equalsIgnoreCase("学号")){
                    for(int i=0;i<persons.length-1;i++){
                        for (int j = i+1; j < persons.length; j++) {
                            if (persons[i].number .compareTo(persons[j].number)>0) {
                                Person p=persons[i];
                                persons[i]=persons[j];
                                persons[j]=p;
                            }
                        }
                    }
                }else if(str.equalsIgnoreCase("姓名")){
                    System.out.println("sss");
                    for(int i=0;i<persons.length-1;i++){
                        for (int j = i+1; j < persons.length; j++) {
                            if (persons[i].name .compareTo(persons[j].name)>0) {
                                Person p=persons[i];
                                persons[i]=persons[j];
                                persons[j]=p;
                            }
                        }
                    }
                }else if(str.equalsIgnoreCase("省份")){
                    for(int i=0;i<persons.length-1;i++){
                        for (int j = i+1; j < persons.length; j++) {
                            if (((String) (persons[i].provinces)).compareTo((String)(persons[j].provinces))>0) {
                                Person p=persons[i];
                                persons[i]=persons[j];
                                persons[j]=p;
                            }
                        }
                    }
                }else if(str.equalsIgnoreCase("院系")){
                    for(int i=0;i<persons.length-1;i++){
                        for (int j = i+1; j < persons.length; j++) {
                            if (persons[i].department .compareTo(persons[j].department)>0) {
                                Person p=persons[i];
                                persons[i]=persons[j];
                                persons[j]=p;
                            }
                        }
                    }
                }
                refresh(persons);
            }else if(e.getSource()==comboxFind){
                String str=(String) comboxFind.getSelectedItem();
                if(str.equalsIgnoreCase("学号"));
            }
        }


        private void sort() {
            for(int i=0;i<persons.length-1;i++){
                for (int j = i+1; j < persons.length; j++) {
                    if (persons[i].member .compareToIgnoreCase(persons[j].number)<0) {
                        Person p=persons[i];
                        persons[i]=persons[j];
                        persons[j]=p;
                    }
                }
            }
        }


        private void refresh(Person[] persons) {
            text.setText("");
            for(int i=0;i<persons.length;i++){
                text.append(persons[i].toString());
                text.append("\n");
            }
        }
    }


    public class Person {
        private String name;
        private String birth;
        private String isMan;
        private Object provinces;
        private Object cities;
        private String department;
        private String major;
        private String number;
        private String member;
        public Person(){


        }
        public Person(String name, String birth, String isMan,
                      Object provinces, Object cities, String department,
                      String major, String number, String menber) {
            super();
            this.name = name;
            this.birth = birth;
            this.isMan = isMan;
            this.provinces = provinces;
            this.cities = cities;
            this.department = department;
            this.major = major;
            this.number = number;
            this.member = menber;
        }
        @Override
        public String toString() {
            return name + "," + birth + ","
                    + isMan + "," + provinces + "," + cities
                    + "," + department + "," + major
                    + "," + number + "," + member ;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public String getBirth() {
            return birth;
        }
        public void setBirth(String birth) {
            this.birth = birth;
        }
        public String getIsMan() {
            return isMan;
        }
        public void setIsMan(String isMan) {
            this.isMan = isMan;
        }
        public Object getProvinces() {
            return provinces;
        }
        public void setProvinces(Object provinces) {
            this.provinces = provinces;
        }
        public Object getCities() {
            return cities;
        }
        public void setCities(Object cities) {
            this.cities = cities;
        }
        public String getDepartment() {
            return department;
        }
        public void setDepartment(String department) {
            this.department = department;
        }
        public String getMajor() {
            return major;
        }
        public void setMajor(String major) {
            this.major = major;
        }
        public String getNumber() {
            return number;
        }
        public void setNumber(String number) {
            this.number = number;
        }
        public String getMember() {
            return member;
        }
        public void setMember(String member) {
            this.member = member;
        }
    }
    public static void main(String[] args) {
        new User();
    }


}
